#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <_matrix.h>

/*
 * print_matrix - print matrix m with @row rows and @col columns
 *
 * return 0 if all are ok, else return -1 and set errno
 */
int print_matrix(const int * m, const int row, const int col)
{
    int i, j;

    if (!m) {
	fprintf(stderr, "print matrix error: NULL matrix!\n");
	errno = EINVAL;
	return -1;
    }

    for (i = 0; i < row; i++)
    {
	for (j = 0; j < col; j++)
	    fprintf(stdout, "%d ", m[i * col + j]);
	fprintf(stdout, "\n");
    }

    return 0;
}

/*
 * transpose_matrix - transpose matrix m with @row rows and @col columns into @trans
 *
 * the caller of this funciton must allocate memory for the transposed matrix @trans.
 * return 0 if succeed, otherwise -1 and set errno
 *
 */
int transpose_matrix(const int * m, const int row, const int col, int * trans)
{
    int i, j;

    if (!m || row <= 0 || col <= 0 || !trans) {
	errno = EINVAL;
	return -1;
    }

    for (i = 0; i < row; i++)
	for (j = 0; j < col; j++)
	    trans[j * row + i] = m[i * col + j];

    return 0;
}
